﻿using System;
using Besture.Common.Definition;
using Besture.UI.ClassHelper;
using DevExpress.Utils;
using DevExpress.Web.ASPxGridView;
using DevExpress.Web.Data;
using Besture.Common;
using Besture.Bll;

namespace Besture.UI.Admin.Content
{
    public partial class PartList : BasePage
    {
        #region 页面加载

        /// <summary>
        /// 初始化页面
        /// </summary>
        protected override void InitialLoad()
        {
            BindData();
            InitAspxGridView(grid);
        }


        /// <summary>
        /// 初始化GridView控件
        /// </summary>
        public static void InitAspxGridView(ASPxGridView gridView)
        {
            gridView.SettingsLoadingPanel.Text = "正在加载中...";
            gridView.Images.SpriteCssFilePath = "/Admin/Themes/DevTheme/DevExOABlue/{0}/sprite.css";
            gridView.Images.LoadingPanelOnStatusBar.Url = "/Admin/Themes/DevTheme/DevExOABlue/GridView/gvLoadingOnStatusBar.gif";
            gridView.Images.LoadingPanel.Url = "/Admin/Themes/DevTheme/DevExOABlue/GridView/Loading.gif";
            gridView.Styles.CssFilePath = "/Admin/Themes/DevTheme/DevExOABlue/{0}/styles.css";
            gridView.Styles.CssPostfix = "DevExOABlue";
            gridView.Styles.Header.ImageSpacing = 5;
            gridView.Styles.Header.SortingImageSpacing = 5;
            gridView.StylesEditors.CalendarHeader.Spacing = 1;
            gridView.StylesEditors.ProgressBar.Height = 25;
            gridView.SettingsEditing.PopupEditFormAllowResize = true;
            gridView.Settings.ShowFooter = true;
            gridView.Settings.ShowFilterRow = true;
            gridView.SettingsLoadingPanel.ShowImage = true;
            gridView.SettingsBehavior.ConfirmDelete = true;
            gridView.SettingsText.ConfirmDelete = "该操作无法恢复，您确认执行删除吗？";
            //隔行换色
            gridView.Styles.AlternatingRow.Enabled = DefaultBoolean.True;
            //删除提示
            gridView.SettingsBehavior.ColumnResizeMode = ColumnResizeMode.NextColumn;
            gridView.Settings.ShowHeaderFilterButton = true;
            gridView.Settings.EnableFilterControlPopupMenuScrolling = true;

            //不显示加载图片
            gridView.SettingsLoadingPanel.ShowImage = true;
            gridView.SettingsPager.PageSize = 10000;
        }

        /// <summary>
        /// 回发
        /// </summary>
        protected override void PostBackLoad()
        {
            BindData();
        }

        /// <summary>
        /// 绑定数据
        /// </summary>
        public void BindData()
        {
            string sql =
                string.Format(
                    @"SELECT t.*,t1.Name AS TypeName FROM PartTree(0)  t LEFT JOIN TBDictionary t1 ON t.TypeID = t1.ID");
            grid.DataSource = QueryDac.GetTable(sql);
            grid.DataBind();
        }

        /// <summary>
        /// AspxGridView回发事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void grid_CustomCallback(object sender, ASPxGridViewCustomCallbackEventArgs e)
        {
            if (e.Parameters == "Refresh")
                grid.DataBind();
        }

        #endregion

        #region 删除数据

        /// <summary>
        /// 删除单条数据
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void grid_RowDeleting(object sender, ASPxDataDeletingEventArgs e)
        {
            e.Cancel = true;
            int ID = Convert.ToInt32(e.Keys[0]);
            PartDac PartDac = new PartDac();
            PartDac.Delete(ID);
            BindData();
        }

        /// <summary>
        /// 删除选中数据
        /// </summary>
        public void DeleteSelect()
        {
            string ids = AspxGridViewHelper.GetCheckID(grid);
            if (string.IsNullOrEmpty(ids))
            {
                return;
            }
            PartDac partDac = new PartDac();
            partDac.DeleteList(ids);
            BindData();
        }

        #endregion
    }
}